<template>
  <div>
    <!-- 质量报告提报规则维护--车厂 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :fetchapi="fetchApi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isSelectColumn="false"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="AllRuleBtn()">规则</multiuse-button>
<!--        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="chexingguizeBtn()">车型规则</multiuse-button>-->
<!--        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="partRulesBtn()">配件规则</multiuse-button>-->
<!--        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="DelearguizeBtn()">经销商规则</multiuse-button>-->
<!--        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="peijianjichelaingBtn()">车辆规则</multiuse-button>-->
        <multiuse-button size="small" type="primary"  :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <!-- 车型规则 -->

    <BaseDialog :visible.sync="chexingguizeFlag" :title="'车型规则'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <chexingguize @close="chexingguizeclose" @save="chexingguizesave"  :rowData="rowData"></chexingguize>
    </BaseDialog>

    <BaseDialog :visible.sync="peijianjichelaingguizeFlag" :title="'车辆规则'" destroyOnClose width="850px" :containerStyle="{height: '250px', overflow: 'auto', paddingBottom: '60px'}">
      <peijianjichelaingguize @close="closeAdd" @save="saveAdd"  :rowData="rowData"></peijianjichelaingguize>
    </BaseDialog>
    <BaseDialog :visible.sync="delearguizeFlag" :title="'经销商选择'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <Deilearguize @close="Delearclose" @save="Delearsave" :rowData="rowData"></Deilearguize>
    </BaseDialog>
    <BaseDialog :visible.sync="visible" :title="'车型选择'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <vehicle @close="close" @save="save" :rowData="rowData"></vehicle>
    </BaseDialog>
    <BaseDialog :visible.sync="partRules" :title="'配件规则'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <PartRules @close="partRulesClose" @save="partRulesSave" :rowData="rowData"></PartRules>
    </BaseDialog>
    <BaseDialog :visible.sync="allRuleFlag" :title="'规则'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <AllRule @close="allRuleClose" @save="allRuleSave" :rowData="rowData"></AllRule>
    </BaseDialog>
  </div>
</template>
<script>
import {getListByPage,getListForExport,exporturl} from '@/api/claim/factory/QualityInformationManagement_OEM/QualityInformationRules/index';
import chexingguize from './SeriesRules.vue';
import peijianjichelaingguize from './CarRules.vue';
import Deilearguize from './DealerRule.vue';
import PartRules from './PartRules.vue';
import vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import AllRule from './AllRule.vue';
import { exportExcelFile } from '@/utils/index';
import { getPartType } from '@/api/dictionary/index';
import { dictionary } from '@/utils/dictMixin';

import moment from "moment";
import { getAppId } from '@/assets/js/auth';
export default {
  mixins: [dictionary],
  components: {
    vehicle,
    chexingguize,
    peijianjichelaingguize,
    Deilearguize,
    PartRules,
    AllRule
  },
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      chexingguizeFlag: false,
      selectDelearFlag: false,
      delearguizeFlag: false,
      peijianjichelaingguizeFlag: false,
      collapse: true,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false,
      partRules: false,
      fetchApi:getListByPage,
      rowData:{},
      allRuleFlag:false,
    };
  },
  created() {
    this.fetchParams.bBeginEffectDate = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.eBeginEffectDate = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.chuqi();
  },
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '规则编号', fieldName: 'ruleCode', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '规则描述', fieldName: 'ruleInfo', placeholder: '请输入', initialValue: '' },
        // { type: 'SELECT', label: '规则范围', fieldName: 'ruleType', placeholder: '请选择', initialValue: '', itemList: [] },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelCode',
          placeholder: '请输入',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          }
        },
        { type: 'INPUT', label: '主损件名称', fieldName: 'partName', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '主损件代码', fieldName: 'partCode', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入', initialValue: '' },
        {
          type: 'RANGE_DATE',
          label: '生效开始日期',
          fieldName: 'bBeginEffectDate|eBeginEffectDate',
          style: { minWidth: '200px' },
          valueFormat: 'yyyy-MM-dd',
          initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],
          rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        {
          type: 'RANGE_DATE',
          label: '生效结束日期',
          fieldName: 'bEndEffectDate|eEndEffectDate',
          style: { minWidth: '200px' },
          valueFormat: 'yyyy-MM-dd'
        },
        // {
        //   type: 'RANGE_DATE',
        //   label: '销售开始日期',
        //   fieldName: 'bSaleBeginDate|eSaleBeginDate',
        //   style: { minWidth: '200px' },
        //   valueFormat: 'yyyy-MM-dd'
        // },
        // {
        //   type: 'RANGE_DATE',
        //   label: '销售结束日期',
        //   fieldName: 'bSaleEndDate|eSaleEndDate',
        //   style: { minWidth: '200px' },
        //   valueFormat: 'yyyy-MM-dd'
        // },
        {
          type: 'RANGE_DATE',
          label: '生产开始日期',
          fieldName: 'bProBeginDate|eProBeginDate',
          style: { minWidth: '200px' },
          valueFormat: 'yyyy-MM-dd'
        },
        {
          type: 'RANGE_DATE',
          label: '生产结束日期',
          fieldName: 'bProEndDate|eProEndDate',
          style: { minWidth: '200px' },
          valueFormat: 'yyyy-MM-dd'
        }
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      if(!val.bBeginEffectDate) {
        this.$notify.warning({ title: '提示信息', message: '生效开始日期不可为空！' });
        return
      }
      if(!val.eBeginEffectDate) {
        this.$notify.warning({ title: '提示信息', message: '生效开始日期不可为空！' });
        return
      }
      if(getAppId() == 'arcfox') {
        if (moment(moment(val.eBeginEffectDate).format('YYYY-MM-DD')).diff(moment(val.bBeginEffectDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '生效日期跨度不可超过十二个月！' });
          return
        }
      } else {
        if (moment(moment(val.eBeginEffectDate).format('YYYY-MM-DD')).diff(moment(val.bBeginEffectDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '生效日期跨度不可超过十二个月！' });
          return
        }
      }
      this.fetchParams = Object.assign({},val);
      console.log(val);
    },
    async chuqi(){
      // console.log("进来了")
      // const res1 = await getPartType( {type:5802});
      // if (res1.resultCode == 200) {
      //   this.topFilterInit.find(x => x.fieldName == 'ruleType').initialValue = '';
      //   this.topFilterInit.find(x => x.fieldName == 'ruleType').itemList = res1.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      // }

    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          render: (props, h) => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.edit(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '规则编号', dataIndex: 'ruleCode', sorter: true },
        { title: '规则描述', dataIndex: 'ruleInfo', sorter: true },
        // { title: '规则范围', dataIndex: 'ruleTypeStr', sorter: true },
        { title: '状态', dataIndex: 'statusStr', sorter: true },
        { title: '生效开始日期', dataIndex: 'beginEffectDate', sorter: true , dateFormat: 'yyyy-MM-dd'},
        { title: '生效结束日期', dataIndex: 'endEffectDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '生产开始日期', dataIndex: 'proBeginDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '生产结束日期', dataIndex: 'proEndDate', sorter: true , dateFormat: 'yyyy-MM-dd'},
        { title: '销售开始时限', dataIndex: 'saleBeginTime', sorter: true},
        { title: '销售结束时限', dataIndex: 'saleEndTime', sorter: true},
        { title: '是否包含批量活动', dataIndex: 'isAct', sorter: true,
          filterItems: this.createDictList('5500'),
          dictItems: this.createDictList('5500')},
        // { title: '销售开始日期', dataIndex: 'saleBeginDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        // { title: '销售结束日期', dataIndex: 'saleEndDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '车型代码', dataIndex: 'modelCode', sorter: true },
        // { title: '主损件代码', dataIndex: 'partCode', sorter: true },
        { title: '断点VIN', dataIndex: 'vin', sorter: true },
        { title: '创建人', dataIndex: 'createdBy', sorter: true },
        { title: '创建日期', dataIndex: 'createdAt', sorter: true, dateFormat: 'yyyy-MM-dd' }
      ];
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    close() {
      this.visible = false;
    },
    chooseOrderNo() {
      this.visible = true;
    },
    save(obj) {
      this.topFilterInit.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
      this.visible = false;
    },
  async  exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000157' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "bBeginEffectDate|eBeginEffectDate").initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == "bBeginEffectDate|eBeginEffectDate").initialValue[1];
      // if(!beginSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '生效开始日期不可为空！' });
      //   return
      // }
      // if(!endSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '生效结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '生效日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '生效日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({},this.fetchParams);
      // obj.bBeginEffectDate = this.topFilterInit.find(x => x.fieldName == "bBeginEffectDate|eBeginEffectDate").initialValue[0];
      // obj.eBeginEffectDate = this.topFilterInit.find(x => x.fieldName == "bBeginEffectDate|eBeginEffectDate").initialValue[1];
      // getListForExport(obj).then(res => {
      //   exportExcelFile(res.data, '质量规则提报');
      // });
    },
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.peijianjichelaingguizeFlag = false;
    },
    saveAdd() {
      this.peijianjichelaingguizeFlag = false;
      this.fetchParams = { ...this.fetchParams };
    },
    editclose() {
      this.visibleEdit = false;
    },
    edit(val) {
      this.rowData = val;
      // if(this.rowData.ruleType==58021001){
      //   this.chexingguizeFlag = true;
      // }else if(this.rowData.ruleType==58021002){
      //   this.partRules = true;
      // }else if(this.rowData.ruleType==58021003){
      //   this.delearguizeFlag = true;
      // }else if(this.rowData.ruleType==58021004){
      //   this.peijianjichelaingguizeFlag = true;
      // }else{
        this.allRuleFlag = true;
      // }

      },
    editsave() {
      this.visibleEdit = false;
      this.fetchParams = { ...this.fetchParams };
    }, //经销商选择页面
    SelectDelear() {
      this.delearguizeFlag = true;
    }, //关闭
    Delearclose() {
      this.delearguizeFlag = false;
    },
    partRulesClose(){
      this.partRules = false;
    },
    partRulesSave(){
      this.partRules = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //大规则
    AllRuleBtn() {
      this.rowData = null;
      this.allRuleFlag = true;
    },
    allRuleSave(){
      this.allRuleFlag = false;
      this.fetchParams = { ...this.fetchParams };
    },
    allRuleClose(){
      this.allRuleFlag = false;
    },
    //保存
    Delearsave() {
      this.delearguizeFlag = false;
      this.fetchParams = { ...this.fetchParams };
    }, //车型规则
    chexingguizeBtn() {
      this.rowData = null;
      this.chexingguizeFlag = true;
    },//配件及车辆
     peijianjichelaingBtn() {
       this.rowData = null;
      this.peijianjichelaingguizeFlag = true;
    },
    partRulesBtn() {
      this.rowData = null;
      this.partRules = true;
    },
    //经销商规则
     DelearguizeBtn() {
       this.rowData = null;
      this.delearguizeFlag = true;
    },
    chexingguizeclose() {
      this.chexingguizeFlag = false;
    },
    chexingguizesave() {
      this.chexingguizeFlag = false;
      this.fetchParams = { ...this.fetchParams };
    }
  }
  // mounted() {
  //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
  // }
};
</script>

<style lang="less" scoped></style>
